#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
#include "parser.h"

using namespace std;



InputData::InputData(const char* inFile) 
	: n(0), 
	total_edges(0),
	ready(false)
{
	int ret;
	ret = processInput(inFile);
}

InputData::InputData(const string inFile) 
	: n(0), 
	total_edges(0),
	ready(false)
{
	char in[BUF_LINE];
	int	 ret;

	strncpy(in, inFile.c_str(), BUF_LINE);
	ret = processInput(in);
}

InputData::~InputData()
{
	if ( n > 0) {
		for (int i = 0; i < n; i++)
			delete [] matrix[i]; /* radky */
			
		delete [] matrix; /* ukazatel na radky */
	}
}


void
InputData::printMatrix()
{
	if (inputReady()) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				cout << matrix[i][j] << " ";
			}
			cout << endl;
		}
	}
}

int
InputData::processInput(const char* inFile)
{
	unsigned n;
	unsigned i = 0, j = 0, l = 0;
	char	 line[BUF_LINE];
		
	ifstream input(inFile, ifstream::in);

	input.getline(line, BUF_LINE);
	if (input.bad() || input.fail() || input.eof()) {
		perror("Error input file");
		return 1;
	}
	
	n = atoi(line);
	this->n = n;
	

	/* vytvor 2-rozmerny pole */
	matrix = new int*[n];
		
	for (i = 0; i < n; i++)
		matrix[i] = new int[n];


	/* naparsuj vstupni matici do pole */
	for (i = 0; i < n; i++) {
		input.getline(line, BUF_LINE);
		if (input.bad() || input.fail() || input.eof())
			return 1;
			
		j = 0;
		l = 0;
		while (line[l] != '\0') {
			if (line[l] == '0') {
				matrix[i][j] = 0;
				j++;
			}
			else if (line[l] == '1') {
				matrix[i][j] = 1;
				j++;
				
				total_edges++;
			}
			l++;

		}
	}
	
	/* pricital jsem hranu pro oba uzly */
	total_edges = total_edges / 2;
	
	input.close();
	
	this->ready = true;
	return 0;
}



